import { Asset, BufferAsset, SpriteFrame, loader, resources } from "cc";
import Singleton from "../Base/Singleton";
import { str2ArrayBuffer, str2Uint8Array } from "../Utils";

export class ResourceManager extends Singleton  {
  static get Instance() {
      return super.GetInstance<ResourceManager>()
  }
  loadDir(path:string,type:typeof SpriteFrame = SpriteFrame){
    return new Promise<SpriteFrame[]>((resolve,reject)=>{
      resources.loadDir(path,type,function(err,assets){
        if(err){
          reject(err)
          return
        }
        resolve(assets)
      })
    })
  }

  loadBinary(path:string){
    return new Promise<Uint8Array>((resolve,reject)=>{
        resources.load
        // loader.loadRes 
        (path,Asset,(err,asset)=>{
            if(err){
                reject(err)
              }else{
                // console .log(str2ArrayBuffer(asset._nativeAsset))
                resolve(str2Uint8Array(asset._nativeAsset))
            

              }

        })
    })
    
  }
}